{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "import numpy as np\n",
    "sys.path.append('..//')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from radio.batchflow import Dataset, Pipeline, DatasetIndex, L, F\n",
    "from radio.batchflow import NumpySampler as NS, Sampler, ConstantSampler as CS\n",
    "from radio import CTImagesAugmentedBatch  # augmentation batch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "dsix = DatasetIndex(np.arange(100))\n",
    "ds = Dataset(index=dsix, batch_class=CTImagesAugmentedBatch)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Augmentation samplers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sh = np.array([32, 64, 64])\n",
    "delta = np.array([16, 24, 24])\n",
    "pos = (NS('n', dim=3) * sh / 4 + sh / 2 + delta // 2).truncate(sh - 1, (0, 0, 0))\n",
    "sz = NS('choice', a=np.arange(7, 13)) * np.array([1, 2, 2])\n",
    "noise = NS('n') * 0.1\n",
    "sp = (1 + 0.3 * NS('n', dim=3)).truncate(low=(0.5, 0.5, 0.5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Augmentation pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pipeline = (Pipeline()\n",
    "            .init_with_ones(sh + delta)\n",
    "            .cutout(positions=L(pos.sample, size=F(len)), sizes=L(sz.sample, size=F(len)))\n",
    "            .apply_noise(noise)\n",
    "            .rotate(angle=30)\n",
    "            .unify_spacing(spacing=(L(sp.sample, size=1)), shape=sh, mode='constant')\n",
    "           )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "bch = (ds >> pipeline).next_batch(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot_2d(arry, clim=(-0.3, 1.3)):\n",
    "    plt.imshow(arry, cmap=plt.cm.gray, clim=clim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from ipywidgets import interact\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d04b19aecd5c4b75864542aaf54a3dc7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=1, description='num', max=2), IntSlider(value=15, description='slc', max…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<function __main__.<lambda>>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "interact(lambda num, slc: plot_2d(bch.get(num, 'images')[slc], clim=(0, 1.3)),\n",
    "         num=(0, len(bch) - 1, 1), slc=(0, len(bch.get(0, 'images')) - 1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
